<?php

namespace Gds\VenteBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Gds\ArticleBundle\Entity\Article;
use Gds\ArticleBundle\Entity\Famille;
use Gds\ArticleBundle\Entity\Departement;
use Gds\ArticleBundle\Entity\Stock;
use Gds\UserBundle\Entity\User;
use Gds\VenteBundle\Entity\Client;
use Gds\VenteBundle\Entity\CategorieRemise;
use Gds\ArticleBundle\Entity\Societe;
use Gds\ArticleBundle\Entity\Unite;
use \Gds\AchatBundle\Entity\StockAchat;
use Gds\AchatBundle\Entity\Fournisseur;

class Articles implements FixtureInterface {

    /**
     * {@inheritDoc}
     */
    public function load(ObjectManager $manager) {
        $familles = array();
        for ($i = 1; $i < 10; $i++) {
            $famille = new Famille();
            $famille->setLibelle("famille$i");
            $famille->setIsSupprimer(false);
            $manager->persist($famille);
            $familles[$i - 1] = $famille;
        }
        $sous_familles = array();
        $cmp = 0;
        for ($i = 1; $i < 10; $i++) {
            for ($j = 1; $j < 10; $j++) {
                $sous_famille = new Famille();
                $sous_famille->setFamilleMere($familles[$i - 1]);
                $sous_famille->setLibelle("sous_famille$i$j");
                $sous_famille->setIsSupprimer(false);
                $manager->persist($sous_famille);
                $sous_familles[$cmp] = $sous_famille;
                $cmp++;
            }
        }
        $societe = new Societe();
        $societe->setClientParDepartement(false);
        $societe->setFournisseurParDepartement(false);
        $societe->setNumeroBonLivraison(1);
        $societe->setNumeroBonRetour(1);
        $societe->setNumeroDevis(1);
        $societe->setNumeroFacture(1);
        $societe->setNumeroFactureAvoir(1);
        $societe->setPrefixeBonLivraison('B.L');
        $societe->setPrefixeBonRetour('B.R');
        $societe->setPrefixeCaisse('Caisse');
        $societe->setNumeroCaisse(1);
        $societe->setPrefixeClient('Client');
        $societe->setPrefixeDevis('Devis');
        $societe->setPrefixeFacture('facture');
        $societe->setPrefixeFactureAvoir('F.A');
        $societe->setPrefixeFournisseur('Fournisseur');
        $societe->setNumeroClient(0);
        $societe->setCodeSecret('0000');
        $societe->setLimiteCredit(1000);
        $societe->setCodeQuantite('qte');
        $societe->setPrefixeAcompte('AC');
        $societe->setNumeroAcompte(0);
        $societe->setPrefixeTicket('TICKET');
        $societe->setNumeroTicket(0);
        $societe->setPrefixeReglement('REG');
        $societe->setNumeroReglement(0);
        $societe->setPrefixeReglementFournisseur('REGFOUR');
        $societe->setNumeroReglementFournisseur(0);
        $societe->setCodeBenefice('ben');
        $societe->setCodeSecret('benq');
        $societe->setTimbreFiscal(0.4);
        $societe->setPrefixeBonCommandeFournisseur('B.C.F');
        $societe->setNumeroBonCommandeFournisseur(0);
        $societe->setPrefixeBonCommandeClient('B.C.C');
        $societe->setNumeroBonCommandeClient(0);
        $societe->setCodeRemise('aaaa');

        $manager->persist($societe);
        $user = new User();
        $user->setEmail('abdellatif.ellouze@gmail.com');
        $user->setUsername('admin');
        $user->setPlainPassword('adminadmin');
        $user->setPhoto('tifo.jpg');
        $user->setNom('ellouze');
        $user->setPrenom('abdellatif');
        $user->setTelephone('52015307');
        $user->setCin('08808052');
        $user->setFunction('Gerant');
        $user->setEnabled(true);
        $user->setRoles(array('ROLE_SUPER_ADMIN'));

        $manager->persist($user);
        $departement = new Departement();
        $departement->setAdresse('route gremda');
        $departement->setNom('sofopel gremda');
        $departement->setIsSupprimer(false);
        $departement->setType('point de vente');
        $departement->setVille('sfax');
        $manager->persist($departement);
        $departement2 = new Departement();
        $departement2->setAdresse('route el ain');
        $departement2->setNom('sofopel ain');
        $departement2->setIsSupprimer(false);
        $departement2->setType('point de vente');
        $departement2->setVille('sfax');
        $manager->persist($departement2);
        $array_articles = array();
        $unite1 = new Unite();
        $unite1->setCode('m');
        $unite1->setLibelle('mettre');
        $unite1->setChiffreVirgule(3);
        $manager->persist($unite1);
        $unite2 = new Unite();
        $unite2->setCode('Kg');
        $unite2->setLibelle('kilogramme');
        $unite2->setChiffreVirgule('2');
        $manager->persist($unite2);
        $unite3 = new Unite();
        $unite3->setCode('Piece');
        $unite3->setLibelle('Piece');
        $unite3->setChiffreVirgule(0);
        $manager->persist($unite3);
        for ($i = 1; $i < 200; $i++) {
            $article = new Article();
            $article->setUnite($unite3);
            $article->setDesignation('designation' . $i);
            $article->setReference('reference' . $i);
            $article->setPrixAchatHt($i / 2);
            $article->setPrixVente($i);
            $article->setRemise(25);
            $article->setIsSupprimer(false);
            $article->setTauxTva(18);
            $article->setCodeBarre('codeAbarre' . $i);
            $cc = $i % 15;
            $article->setFamille($sous_familles[$cc]);
            $article->setRemiseMax(40);
            $manager->persist($article);
            $stock1 = new Stock();
            $stock1->setDepartement($departement);
            $stock1->setQuantiteBlanc(1);
            $stock1->setQuantiteNoir(10);
            $stock1->setQuantiteMin(0);
            $stock1->setArticle($article);
            $manager->persist($stock1);
            $stock_achat1 = new StockAchat();
            $stock_achat1->setStock($stock1);
            $stock_achat1->setQuantite(1);
            $stock_achat1->setIsNoir(false);
            $stock_achat1->setPrixAchatTtc($i / 2);
            $manager->persist($stock_achat1);
            $stock_achat2 = new StockAchat();
            $stock_achat2->setStock($stock1);
            $stock_achat2->setQuantite(6);
            $stock_achat2->setIsNoir(true);
            $stock_achat2->setPrixAchatTtc($i / 3);
            $manager->persist($stock_achat2);
            $stock_achat3 = new StockAchat();
            $stock_achat3->setStock($stock1);
            $stock_achat3->setQuantite(4);
            $stock_achat3->setIsNoir(true);
            $stock_achat3->setPrixAchatTtc($i / 4);
            $manager->persist($stock_achat3);
            $stock2 = new Stock();
            $stock2->setDepartement($departement2);
            $stock2->setQuantiteBlanc(2);
            $stock2->setQuantiteNoir(20);
            $stock2->setQuantiteMin(0);
            $stock2->setArticle($article);
            $manager->persist($stock2);
            $stock_achat1 = new StockAchat();
            $stock_achat1->setStock($stock2);
            $stock_achat1->setQuantite(2);
            $stock_achat1->setIsNoir(false);
            $stock_achat1->setPrixAchatTtc($i / 2);
            $manager->persist($stock_achat1);
            $stock_achat2 = new StockAchat();
            $stock_achat2->setStock($stock2);
            $stock_achat2->setQuantite(12);
            $stock_achat2->setIsNoir(true);
            $stock_achat2->setPrixAchatTtc($i / 3);
            $manager->persist($stock_achat2);
            $stock_achat3 = new StockAchat();
            $stock_achat3->setStock($stock1);
            $stock_achat3->setQuantite(8);
            $stock_achat3->setIsNoir(true);
            $stock_achat3->setPrixAchatTtc($i / 4);
            $manager->persist($stock_achat3);
            $array_articles[$i] = $article;
        }
        $manager->flush();
        for ($i = 0; $i < 100; $i++) {
            $client = new Client();
            $client->setCode("Client-" . $i);
            $client->setIsActive(true);
            $client->setAdresse("adresse" . $i);
            $client->setFax("fax");
            $client->setGsm("gsm");
            $client->setNom("nom$i");
            $client->setPrenom("prenom$i");
            $client->setLimiteCredit(1000);
            $client->setNumeroExonoration("exo" . $i);
            $client->setMatriculeFiscale("matriculeFiscale" . $i);
            $client->setRaisonSocial("raison" . $i);
            $client->setSolde($i);
            $client->setCodeBarre('000000');
            $client->setTel("tel");
            $manager->persist($client);
            for ($j = 0; $j < 10; $j++) {
                $remise = round(($j + $i) % 100, 2);
                $categorie_remise = new CategorieRemise();
                $article = $manager->getRepository('GdsArticleBundle:Article')->findOneById($j + 1);
                $categorie_remise->setArticle(($array_articles[$j + 1])); //($array_articles[$j]);
                $categorie_remise->setClient($client);
                $categorie_remise->setRemise($remise);
                $manager->persist($categorie_remise);
            }
        }
        for ($i = 0; $i < 100; $i++) {
            $fournisseur = new Fournisseur();
            $fournisseur->setCode("Fournisseur-" . $i);
            $fournisseur->setAdresse("adresse" . $i);
            $fournisseur->setFax("fax");
            $fournisseur->setNumeroExonoration("exo" . $i);
            $fournisseur->setMatriculeFiscale("matriculeFiscale" . $i);
            $fournisseur->setRaisonSocial("four" . $i);
            $fournisseur->setSolde($i);
            $fournisseur->setFormeJuridique('SARL');
            $fournisseur->setTel("tel");
            $manager->persist($fournisseur);
        }
        $manager->flush();
        $articles = $manager->getRepository('GdsArticleBundle:Article')->findAll();
        $clients = $manager->getRepository('GdsVenteBundle:Client')->findAll();
        foreach ($articles as $article) {
            foreach ($clients as $client) {
                $remise = 0.1;
                $categorie_remise = new CategorieRemise();
                $categorie_remise->setArticle($article);
                $categorie_remise->setClient($client);
                $categorie_remise->setRemise($remise);
                $manager->persist($categorie_remise);
                $remise+=0.1;
            }
        }

        $manager->flush();
    }

}